﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace SachOnlineLibrary.DAL
{

    class PhieuDatHangDAL
    {
        KetNoiDB ketnoi;
        internal PhieuDatHangDAL() 
        {
            ketnoi = new KetNoiDB();
        }
        internal DataTable LietKeTatCaDonDatHang()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from PhieuDatHang";
            return ketnoi.ExecuteQuery(cmd);
        }

        internal bool CapNhatDonHang(int maphieu, string trangthai, DateTime ngaygiaohang)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update PhieuDatHang set trangthai=@trangthai, ngaygiaohang=@ngaygiaohang where maphieu=@maphieu";
            cmd.Parameters.Add("@trangthai", SqlDbType.NVarChar).Value = trangthai;
            cmd.Parameters.Add("@ngaygiaohang", SqlDbType.DateTime).Value = ngaygiaohang;
            cmd.Parameters.Add("@maphieu", SqlDbType.Int).Value = maphieu;
            return ketnoi.ExecuteNonQuery(cmd);
        }
        internal bool CapNhatDonHang(int maphieu, string trangthai)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update PhieuDatHang set trangthai=@trangthai where maphieu=@maphieu";
            cmd.Parameters.Add("@trangthai", SqlDbType.NVarChar).Value = trangthai;
            cmd.Parameters.Add("@maphieu", SqlDbType.Int).Value = maphieu;
            return ketnoi.ExecuteNonQuery(cmd);
        }
        public bool Them_PhieuDatHang(int tkid, string ten, string sdt, string diachi)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert into phieudathang values(@tkid,@ngay,0,N'Chưa giao',@diachi,NULL,@ten,@sdt)";
            cmd.Parameters.Add("@tkid", SqlDbType.Int).Value = tkid;
            cmd.Parameters.Add("@ngay", SqlDbType.SmallDateTime).Value = DateTime.Now;
            cmd.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = diachi;
            cmd.Parameters.Add("@ten", SqlDbType.NVarChar).Value = ten;
            cmd.Parameters.Add("@sdt", SqlDbType.VarChar).Value = sdt;
            return ketnoi.ExecuteNonQuery(cmd);
        }
        internal int LayIDMax()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select Max(MaPhieu) from PhieuDatHang";
            return int.Parse(ketnoi.ExecuteScalar(cmd));
        }
    }
}
